<?php

require_once 'SecurityController.php';
class EventosController extends SecurityController
{

    public function init()
    {
        parent::init();
    }

    public function indexAction()
    {
    }

    public function porcinoAction()
    {
        $request = $this->getRequest();
        $id_porcino = $request->getParam('id');

        $json = $request->getParam('type');

        $mapper = new Application_Model_EventoMapper();
        $entries = $mapper->fetchByPorcino($id_porcino);

        if ($json != null) {
            $data = array();
            if (!is_array($entries)) {
                array_push($data, $entries->toArray());
            } else {
                foreach ($entries as $entry) {
                    array_push($data, $entry->toArray());
                }
            }
            echo Zend_Json_Encoder::encode(array('rows' => $data));
            exit();
        } else {
            $this->view->entries = $entries;
        }
    }

    public function formAction()
    {
        $request = $this->getRequest();
        $id_event_type = $request->getParam('id_tipo_evento');
        $id_porcino = $request->getParam('id_porcino');

        $form = $this->getTipoEventoForm($id_event_type);

        if ($form != null) {
//            $form->setAction($this->url(array('controller' => "eventos", 'action' => 'save'), 'default', true));
            $form->setAction("/eventos/save");
            $form->getElement("tipo_evento_id")->setValue($id_event_type);
            $form->getElement("porcino_id")->setValue($id_porcino);
            //parent::formAction($form, new Application_Model_Evento(), new Application_Model_EventoMapper());
            //$this->view->form = $form;
            echo $form;
            exit();

        }
    }

    public function eventAction()
    {
        $id_event = $this->getRequest()->getParam('id_evento');
        $id_tipo = $this->getRequest()->getParam('id_tipo');

        $mapper = $this->getEventoExtended($id_tipo);
        $event = $mapper->find($id_event);
        if ($event != null) {
            $toArray = $event->toArray();
        } else {
            $toArray = array();
        }
        echo Zend_Json_Encoder::encode($toArray);
        exit();
    }

    public function saveAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id = $request->getParam('id');
            $id_event_type = $request->getParam('tipo_evento_id');
            $id_porcino = $request->getParam('porcino_id');

            $extendedForm = null;
            $extendedMapper = null;
            $extendedModel = null;

            $mapper = new Application_Model_TipoEventoMapper();
            $event_type = $mapper->find($id_event_type);

            switch ($event_type->getCodigo()) {
                case 'h_monta_natural':
                    $extendedMapper = new Application_Model_EventoMontaNaturalMapper();
                    $extendedModel = new Application_Model_EventoMontaNatural();
                    $extendedForm = new Application_Form_EventoMontaNatural();
                    break;
                case 'h_parto':
                    $extendedMapper = new Application_Model_EventoPartoMapper();
                    $extendedModel = new Application_Model_EventoParto();
                    $extendedForm = new Application_Form_EventoParto();
                    break;
                case 'h_celo_no_servido':
                    $extendedMapper = new Application_Model_EventoCeloNoServidoMapper();
                    $extendedModel = new Application_Model_EventoCeloNoServido();
                    $extendedForm = new Application_Form_EventoCeloNoServido();
                    break;
                case 'h_destete':
                    $extendedMapper = new Application_Model_EventoDesteteMapper();
                    $extendedModel = new Application_Model_EventoDestete();
                    $extendedForm = new Application_Form_EventoDestete();
                    break;
            }


            if ($extendedForm->isValid($request->getPost())) {
                if ($id != null) {
                    //Update only for values data
                    $values = $extendedForm->getValues();
                } else {
                    $mapper = new Application_Model_EventoMapper();
                    $event = new Application_Model_Evento($extendedForm->getValues());
                    $id = $mapper->save($event);

                    $extendedForm->getElement('id')->setValue($id);
                    $values = new $extendedModel($extendedForm->getValues());
                }

                $extendedMapper->getDbTable()->insert($values->toArray());
                exit();
            }
        }
    }

    public function  getTipoEventoForm($id_event_type)
    {
        $form = null;
        $mapper = new Application_Model_TipoEventoMapper();
        $event_type = $mapper->find($id_event_type);

        switch ($event_type->getCodigo()) {
            case 'h_monta_natural':
                $form = new Application_Form_EventoMontaNatural();
                break;
            case 'h_parto':
                $form = new Application_Form_EventoParto();
                break;
            case 'h_celo_no_servido':
                $form = new Application_Form_EventoCeloNoServido();
                break;
            case 'h_destete':
                $form = new Application_Form_EventoDestete();
                break;
        }
        $form->getElement('tipo_evento_id')->setValue($id_event_type);
        return $form;
    }

    public function  getEventoExtended($id_event_type)
    {

        $mapper = new Application_Model_TipoEventoMapper();
        $event_type = $mapper->find($id_event_type);

        switch ($event_type->getCodigo()) {
            case 'h_monta_natural':
                return new Application_Model_EventoMontaNaturalMapper();
            case 'h_parto':
                return new Application_Model_EventoPartoMapper();
            case 'h_celo_no_servido':
                return new Application_Model_EventoCeloNoServidoMapper();
            case 'h_destete':
                return new Application_Model_EventoDesteteMapper();
        }
        return null;
    }


}







